#include <game.h>

inventaire::inventaire() : base("inventaire")
{
	
}

inventaire::~inventaire()
{
	
}

int inventaire::init(int i,coeur *c,IGUIEnvironment *e,stringc path,position2d<s32> pos,int v,INVENTAIRE_POS p)
{
	base::init(i,c);
	env = e;
	
	vtot = v;
	volume=0;

	position = p;

	image = env->addImage(mycore->loadTexture(path),pos,true);
	ITexture* texvoid = mycore->loadTexture(stringc("texture/void.png"));

	switch (p)
	{
	case INVENTAIRE_POS_GAUCHE:
		maxItem=6;
		it = new item* [6];
		images = new IGUIImage* [6];
		for (int i=0;i<6;i++)
		{
			images[i] = env->addImage(texvoid,position2d<s32>(10,110+90*i),true,image);
			images[i]->setImage(0);
			it[i]=0;
		}
		break;
	case INVENTAIRE_POS_DROITE:
		maxItem=6;
		it = new item* [6];
		images = new IGUIImage* [6];
		for (int i=0;i<6;i++)
		{
			images[i] = env->addImage(texvoid,position2d<s32>(10,110+90*i),true,image);
			images[i]->setImage(0);
			it[i]=0;
		}
		break;
	case INVENTAIRE_POS_HAUT:
		maxItem=8;
		it = new item* [8];
		images = new IGUIImage* [8];
		for (int i=0;i<8;i++)
		{
			images[i] = env->addImage(texvoid,position2d<s32>(934,110+90*i),true,image);
			images[i]->setImage(0);
			it[i]=0;
		}
		break;
	case INVENTAIRE_POS_BAS:
		maxItem=10;
		it = new item* [10];
		images = new IGUIImage* [10];
		for (int i=0;i<10;i++)
		{
			images[i] = env->addImage(texvoid,position2d<s32>(110+90*i,10),true,image);
			images[i]->setImage(0);
			it[i]=0;
		}
		break;
	}

	afficher(false);
	return 1;
}

int inventaire::update()
{
	return 1;
}

int inventaire::close()
{
	base::close();
	return 1;
}

int inventaire::afficher(bool a)
{
	image->setVisible(a);
	return 1;
}

IGUIImage* inventaire::getImage()
{
	return image;
}

int inventaire::addItem(int n,item* m)
{
	if (((n<0)||(n>maxItem))||(it[n]))
		return -1;
	it[n] = m;
	images[n]->setImage(m->getTexture());
	return n;
}

int inventaire::removeItem(int n)
{
	if ((n<0)||(it[n]==0)||(n>maxItem))
		return -1;
	it[n] = 0;
	images[n]->setImage(0);
	return n;
}

int inventaire::getNumItem(const position2d<s32> pos)
{
	for (int i=0;i<maxItem;i++)
		if (images[i]->isPointInside(pos))
			return i;
	return -1;
}

item* inventaire::getItem(int n)
{
	if ((n<0)||(n>maxItem))
		return 0;
	return it[n];
}

item* inventaire::getItem(const position2d<s32> pos)
{
	return it[getNumItem(pos)];
}

int inventaire::getFreePlace()
{
	for (int i=0;i<maxItem;i++)
		if (it[i]==0)
			return i;
	return -1;
}

bool inventaire::OnEvent(const SEvent& event)
{
	return false;
}

